import { defineStore } from "pinia";
import { useClearStorageSync } from "@/utils/storageSync";

const actions = () => {
  const initialState = ref<useUserStoreType.stateType>({
    token: "user:normal:0240363e7b1d4e3eabb0eb6941725487",
    userInfo: {}
  });

  const setUserInfo = <T extends Record<string, any>>(payload: T) => {
    initialState.value.userInfo = payload ?? {};
  };

  const setToken = (payload: string) => {
    initialState.value.token = payload;
  };

  const signOut = () => {
    initialState.value = {
      token: "",
      userInfo: {}
    };
    useClearStorageSync();
  };

  return { initialState, setUserInfo, setToken, signOut };
};

export const useUserStore = defineStore("useUserStore", () => actions(), { persist: true });
